package com.fr.web.core.chwriter;

import com.fr.base.ArrayUtils;
import com.fr.base.CodeUtils;
import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.Painter;
import com.fr.base.Style;
import com.fr.base.core.DateUtils;
import com.fr.base.core.html.Tag;
import com.fr.base.core.html.TextHtml;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.chart.ChartPainter;
import com.fr.data.core.db.BinaryObject;
import com.fr.data.impl.NameWidget;
import com.fr.privilege.authentication.Authentication;
import com.fr.report.CellElement;
import com.fr.report.ReportGetter;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.Present;
import com.fr.report.cellElement.TextFormat;
import com.fr.report.cellElement.core.CellElementAttribute;
import com.fr.report.cellElement.core.CellUtils;
import com.fr.report.core.PaintUtils;
import com.fr.report.web.ui.DateEditor;
import com.fr.report.web.ui.Widget;
import com.fr.report.web.ui.WidgetConfig;
import com.fr.report.web.ui.WidgetManager;
import com.fr.web.Repository;
import com.fr.web.core.CellBackgroundDimension;
import com.fr.web.core.HTMLWriterUtils;
import java.awt.Image;
import java.awt.Rectangle;
import java.sql.Blob;
import java.sql.Clob;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WriteCellWriter extends CellHtmlWriter
{
  public WriteCellWriter(int paramInt, Repository paramRepository)
  {
    super(paramRepository, paramInt);
  }

  public static ColumnRow[] retriveCellElementPositions(CellElement paramCellElement)
  {
    if ((paramCellElement.getColumnSpan() <= 1) && (paramCellElement.getRowSpan() <= 1))
      return new ColumnRow[] { ColumnRow.valueOf(paramCellElement.getColumn(), paramCellElement.getRow()) };
    int i = paramCellElement.getRow();
    int j = paramCellElement.getColumn();
    int k = paramCellElement.getRowSpan();
    int l = paramCellElement.getColumnSpan();
    ArrayList localArrayList = new ArrayList();
    for (int i1 = i; i1 < i + k; ++i1)
      for (int i2 = j; i2 < j + l; ++i2)
        localArrayList.add(ColumnRow.valueOf(i2, i1));
    return ((ColumnRow[])localArrayList.toArray(new ColumnRow[localArrayList.size()]));
  }

  public static String generateTdPositionAttr(ColumnRow[] paramArrayOfColumnRow, int paramInt)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < paramArrayOfColumnRow.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append('-');
      localStringBuffer.append(paramArrayOfColumnRow[i] + "-" + paramInt);
    }
    return localStringBuffer.toString();
  }

  public Tag cell2Tag(CellElement paramCellElement, ReportGetter paramReportGetter, Rectangle paramRectangle1, Rectangle paramRectangle2)
  {
    Tag localTag = null;
    try
    {
      localTag = super.cell2Tag(paramCellElement, paramReportGetter, paramRectangle1, paramRectangle2);
    }
    catch (JSONException localJSONException1)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException1.getMessage(), localJSONException1);
    }
    ColumnRow[] arrayOfColumnRow = retriveCellElementPositions(paramCellElement);
    Object localObject1 = paramCellElement.getAttribute(CellElementAttribute.HTML_ID);
    if (localObject1 != null)
      if (localObject1 instanceof ColumnRow[])
        arrayOfColumnRow = (ColumnRow[])localObject1;
      else
        return this.resTag;
    this.resTag.attr("id", arrayOfColumnRow[0] + "-" + this.reportIndex + "-" + this.tableID);
    this.resTag.attr("position", generateTdPositionAttr(arrayOfColumnRow, this.reportIndex));
    if (Boolean.TRUE.equals(paramCellElement.getAttribute(CellElementAttribute.DEL)))
    {
      this.resTag.cls("del");
      return this.resTag;
    }
    Authentication localAuthentication = this.repo.getCurrentAuthentication();
    Widget localWidget = paramCellElement.getWidget();
    if (localWidget != null)
    {
      this.resTag.attr("width", this.cellWidth + "");
      this.resTag.attr("height", this.cellHeight + "");
      try
      {
        String str1 = widget2Config(localWidget, paramCellElement.getValue(), arrayOfColumnRow[0], paramCellElement, this.reportIndex, null);
        if (localWidget instanceof NameWidget)
          localWidget = FRContext.getWidgetManager().getWidgetConfig(((NameWidget)localWidget).getName()).toWidget();
        if (localWidget.isEditor())
          this.resTag.attr("editor", str1);
        else
          this.resTag.attr("widget", str1);
      }
      catch (JSONException localJSONException2)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException2.getMessage(), localJSONException2);
      }
    }
    Style localStyle = paramCellElement.getStyle();
    Object localObject2 = localStyle.getFormat();
    if ((localObject2 == null) && (localWidget instanceof DateEditor))
    {
      localObject3 = ((DateEditor)localWidget).getFormatText();
      localObject2 = new SimpleDateFormat((String)localObject3);
    }
    Object localObject3 = paramCellElement.getPresent();
    if (localObject3 != null)
    {
      this.resTag.cls("presentable");
      localObject4 = ((Present)localObject3).dependence();
      if (!(ArrayUtils.isEmpty(localObject4)))
        try
        {
          JSONArray localJSONArray = new JSONArray(localObject4);
          this.resTag.attr("presentDep", localJSONArray.toString());
        }
        catch (JSONException localJSONException3)
        {
          FRContext.getLogger().log(Level.WARNING, localJSONException3.getMessage(), localJSONException3);
        }
    }
    Object localObject4 = paramCellElement.getValue();
    if (localObject4 instanceof Formula)
    {
      this.resTag.attr("fm", ((Formula)localObject4).getContent().substring(1));
      localObject4 = dealWithValue4Formula(localObject4);
    }
    if (localObject4 != null)
    {
      if ((localObject4 instanceof Date) && (localObject2 == null))
        localObject2 = DateUtils.DATEFORMAT1;
      else if ((localObject2 == null) && (localObject4 instanceof String) && (localObject4.toString().length() > 0))
        localObject2 = TextFormat.getInstance();
      if (localObject4 instanceof Blob)
        localObject4 = dealWithValue4Blob(localObject4);
      if (localObject4 instanceof BinaryObject)
        localObject4 = dealBinaryObject(localObject4);
      if (localObject4 instanceof Clob)
        localObject4 = dealClobValue(localObject4);
      try
      {
        this.resTag.attr("cv", CodeUtils.jsonEncode(CodeUtils.object2JSONable(localObject4)));
      }
      catch (JSONException localJSONException4)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException4.getMessage(), localJSONException4);
      }
      if (this.cellGUIAttr.isPreviewContent())
      {
        if (localObject3 != null)
          localObject4 = paramCellElement.getAttribute(CellElementAttribute.PRESENT_VALUE);
        if (localObject4 instanceof ChartPainter)
        {
          HTMLWriterUtils.value2PaintableTag(localObject4, paramCellElement.getStyle(), this.cellWidth, this.cellHeight, localTag, this.repo, false);
          localStyle = localStyle.derivePadding(0, 0);
        }
        else if ((localStyle.getVerticalText() == 1) || (localObject4 instanceof Image) || (localObject4 instanceof Painter))
        {
          this.repo.writeTagBackground(localTag, CellUtils.value2Image(localObject4, localStyle, this.cellWidth, this.cellHeight));
          localStyle = localStyle.derivePadding(0, 0);
        }
        else if ((this.cellHeight > 0) && (this.cellWidth > 0))
        {
          String str2 = PaintUtils.valueToText(localObject4, (Format)localObject2);
          if (!(this.cellGUIAttr.isShowAsHTML()))
            str2 = CodeUtils.htmlEncode(str2);
          if ((localWidget == null) || (localWidget.isEditor()))
            localTag.sub(new TextHtml(str2));
        }
      }
    }
    if (localObject2 instanceof SimpleDateFormat)
      this.resTag.attr("fmt", "D" + ((SimpleDateFormat)localObject2).toPattern().trim());
    else if (localObject2 instanceof DecimalFormat)
      this.resTag.attr("fmt", ((DecimalFormat)localObject2).toPattern().trim());
    else if (localObject2 == TextFormat.getInstance())
      this.resTag.attr("fmt", "T");
    if (!(this.cellGUIAttr.isPreviewContent()))
    {
      this.resTag.cls("cehide");
      return this.resTag;
    }
    if ((paramReportGetter.getRowHeight(paramCellElement.getRow()) != 0) && (paramReportGetter.getColumnWidth(paramCellElement.getColumn()) != 0))
      HTMLWriterUtils.style_background2css(localStyle, this.resTag, this.cellGUIAttr, new CellBackgroundDimension(paramCellElement, paramReportGetter), this.repo);
    return ((Tag)(Tag)(Tag)this.resTag);
  }
}